package com.xz.xzutils.actions;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationGroupManager;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.DefaultProjectFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.BalloonBuilder;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.wm.StatusBar;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.ui.awt.RelativePoint;
import lombok.val;

import java.util.Objects;

/**
 * <p>
 *
 * </p>
 *
 * @author DONG.WANG17@HAND-CHINA.COM
 * @since 2025-01-09 22:09
 */
public class CopyCurrentLineAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        Editor editor = e.getData(CommonDataKeys.EDITOR);
        if (editor != null) {
            copyCurrentLineToClipboard(editor);
        }
    }
    // 复制当前行到剪贴板
    private void copyCurrentLineToClipboard(Editor editor) {
        // 获取当前光标
        CaretModel caretModel = editor.getCaretModel();
        int lineNumber = caretModel.getLogicalPosition().line;

        // 获取文档内容
        Document document = editor.getDocument();
        int startOffset = document.getLineStartOffset(lineNumber);
        int endOffset = document.getLineEndOffset(lineNumber);

        // 获取当前行的文本
        String currentLineText = document.getText(new TextRange(startOffset, endOffset));

        // 将文本复制到剪贴板
        java.awt.datatransfer.StringSelection stringSelection = new java.awt.datatransfer.StringSelection(currentLineText);
        java.awt.Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);

        // 提示用户已复制
        // Messages.showInfoMessage(editor.getProject(), "当前行已复制到剪贴板", "复制成功");

        // 显示右下角提示
        // https://juejin.cn/post/7313538348404867113
        Notification notification =new Notification("XzUtilsMessage", "复制成功", NotificationType.INFORMATION);
        Notifications.Bus.notify(notification);

        //NotificationGroupManager.getInstance()
        //        .getNotificationGroup("XzUtilsMessage")
        //        .createNotification("XzUtils", "复制成功", NotificationType.INFORMATION)
        //        .notify(editor.getProject());
    }
}
